package edu.scau.DAO_mode.ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JPopupMenu;

import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.util.List;

import javax.swing.JMenuItem;

import edu.scau.DAO_mode.model.User;
import edu.scau.DAO_mode.serviceimp.UserServiceImp;
import edu.scau.DAO_mode.util.Error;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MainFrame extends JFrame {

	private JPanel contentPane;
	private static JTable userTable;
	private static int rowClicked;

	/**
	 * Launch the application.
	 * 
	 * public static void main(String[] args) { EventQueue.invokeLater(new
	 * Runnable() { public void run() { try { MainDialog frame = new
	 * MainDialog(); frame.setVisible(true); } catch (Exception e) {
	 * e.printStackTrace(); } } }); }
	 */
	/**
	 * Create the frame.
	 * @throws Exception 
	 */
	public MainFrame() throws Exception {
		setTitle("用户列表");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 449, 457);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(50, 28, 342, 358);
		contentPane.add(scrollPane);

		userTable = new JTable();
		userTable.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null},
			},
			new String[] {
				"ID", "\u7528\u6237\u540D", "\u5BC6\u7801"
			}
		));
		scrollPane.setViewportView(userTable);
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(userTable, popupMenu);

		JMenuItem delectMenuItem = new JMenuItem("删除"); //删除user表中数据
		delectMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				UserServiceImp userServiceImp = null;
				try {
					userServiceImp = new UserServiceImp();
					if (userServiceImp.deleteUser(userServiceImp.returnUser((String)userTable.getValueAt(rowClicked, 1)))) 
						JOptionPane.showMessageDialog(null, "delete successful");
				} catch (Exception e) {
					e.printStackTrace();
				}


			}
		});
		popupMenu.add(delectMenuItem);

		JMenuItem updateMenuItem = new JMenuItem("修改");
		updateMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				UserServiceImp userServiceImp = null;
				try{
				userServiceImp = new UserServiceImp();
				User user=userServiceImp.returnUser((String)userTable.getValueAt(rowClicked, 1));
				UpdateDialog updateDialog=new UpdateDialog(user);
				updateDialog.setVisible(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
			}
				
		});
		popupMenu.add(updateMenuItem);
		
		//获取user表数据
		UserServiceImp userServiceImp=new UserServiceImp();
		Object[][] date=null;
		try {
			List<User> list=userServiceImp.findallUser();
			date=new Object[list.size()][3];
			for(int i=0;i<list.size();i++)
			{
				User user=list.get(i);
				date[i][0]=user.getID();
				date[i][1]=user.getUserName();
				date[i][2]=user.getPassword();
			}
			userTable.setModel(new DefaultTableModel(
					date,
					new String[] {
						"ID", "\u7528\u6237\u540D", "\u5BC6\u7801"
					}
				));
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
				rowClicked=e.getY()/userTable.getRowHeight();
			}
		});
	}
}
